<%@ page import="java.sql.*" %>
<%@ page import="java.util.Calendar" %><%--
  Created by IntelliJ IDEA.
  User: 李学健
  Date: 2024/3/21
  Time: 9:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>投票管理</title>
</head>
<body>
<%--居中标签--%>
<h1 style="color: #3880c2">
    <a href="index.jsp" style="color: crimson">返回主页面</a>
</h1>
<center>
    <h3 style="vertical-align: middle ;width:100% ">
        <%

            request.setCharacterEncoding("UTF-8");
            try {

                /* 获取明天的日期代码->*/
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new java.util.Date());
                // 将日期减一天
                calendar.add(Calendar.DAY_OF_YEAR, -1);
                java.util.Date tomorrow = calendar.getTime();
                /*获取明天的日期代码->*/


                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
                PreparedStatement preparedStatement = connection.prepareStatement("select * from vote ");
                ResultSet resultSet = preparedStatement.executeQuery();
                if (!resultSet.next()) {
                    out.print("暂无数据!");
                } else {
                    try {
                        out.write("已创建的投票:<br>");
                        //查询全部也可用,先do再while循环
                        //展示所有已经创建投票，只展示标题，附带跳转页面效果，自带id
                        do {
                            int id = resultSet.getInt(1);
                            String title = resultSet.getString(2);
                            Date date = resultSet.getDate(3);
                            out.write("<a href=\"adminVoteOption.jsp?id=" + id + "\">" + title + "</a>\n");
                            //查看它是否过期,获取到的日期早于当前(仅仅精确到日期，如果相等则过期)
                            if (date.before(tomorrow)) {
                                out.write("<a style=\"color: red\">已过期</a>\n");
                            } else {
                                out.write("<a style=\"color:#e000ff\">未过期</a>\n");
                            }
                            out.write("<br>");
                        } while (resultSet.next());
                    } catch (Exception e) {
                        out.print(e.getMessage());
                        e.printStackTrace();
                    }
                }
                preparedStatement.close();
                connection.close();
            } catch (Exception e) {
                out.print(e.getMessage());
                e.printStackTrace();
            }
        %>
        <a href="adminPage.jsp"></a>
    </h3>
    <h3 style="color: #5eda56;margin-top: 150px">
        新增投票
    </h3>
    <form method="post">
        标题： <input name="title" type="text"><br>
        过期日期：<input type="date" id='time' name="date"/>
        <br>
        <input type="submit" name="submit"<%--这个name要写一下，不然request找不到--%> value="创建">
        <input type="reset" value="重置">
    </form>
    <script>
        //代码获取当前时间找到input的id赋初值为今天
        /*获取当前时间*/
        var time = new Date();
        /*格式化日，如果小于9，则补0*/
        var day = ("0" + time.getDate()).slice(-2);
        /*格式化月，如果小于9，则补0*/
        var month = ("0" + (time.getMonth() + 1)).slice(-2);
        /*拼接日期*/
        var today = time.getFullYear() + "-" + (month) + "-" + (day);
        /*完成赋值操作*/
        document.getElementById("time").value = today;
    </script>
    <%
        //存入标题，内容.
        if (request.getParameter("submit") != null) {
            String title = request.getParameter("title");
            Date date = Date.valueOf(request.getParameter("date"));
            if (!title.equals("")) {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
                    PreparedStatement preparedStatement = connection.prepareStatement("insert into  vote (title,time_limit) values (?,?)");
                    preparedStatement.setString(1, title);
                    preparedStatement.setDate(2, date);
                    int i = preparedStatement.executeUpdate();
                    //看其查到的第一个有没有东西
                    if (i > 0) {
                        //获取自动增加的id号
                        out.print("保存成功!");
                    } else {
                        out.print("失败！");
                    }
                    preparedStatement.close();
                    connection.close();
                } catch (Exception e) {
                    out.print(e.getMessage());
                    e.printStackTrace();
                }
            }
            //刷新页面,且不需要参数
            response.sendRedirect("voteManagement.jsp");
        }

    %>
</center>

</body>
</html>
